import type { Module } from 'vuex'
import type { CurrentStatus, RootStateTypes } from '../interface'
import { size2Rem, assignOwnProp } from '@jsjn/utils'

const currentStatus: Module<CurrentStatus, RootStateTypes> = {
    namespaced: true,
    state() {
        return {
            currentPath: '',
            routeLoading: false,
            screenRatio: 1,
            currentBrowser: null,
            currentPageDepth: size2Rem(1300)
        }
    },
    mutations: {
        setCurrentPath(state, path: string) {
            state.currentPath = path
        },
        setRouteLoading(state, status: boolean) {
            state.routeLoading = status
        },
        setScreenRatio(state, ratio: number) {
            if (state.screenRatio === ratio) return
            state.screenRatio = ratio
        },
        setCurrentBrowser(state, browserInfo: CurrentStatus['currentBrowser']) {
            const currentState = JSON.stringify(state.currentBrowser)
            const nextState = JSON.stringify(browserInfo)
            if (currentState === nextState) return
            state.currentBrowser = browserInfo
        },
        // 全量赋值
        setCurrentStatusStoreModule(state, data: CurrentStatus) {
            const currentState = JSON.stringify(state)
            const nextState = JSON.stringify(data)
            if (currentState === nextState) return
            assignOwnProp(state, data)
        }
    },
    actions: {}
}

export default currentStatus
